<template>
	<view class="account-edit">
		<!-- 用户名 修改 -->
		<view class="edit-username" v-if="action==='username'">
			<input type="text" v-model="username" :placeholder="userInfo.username"/>
			<view class="btn">
				<button type="default" @click="updateUsername()">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState,mapActions} from "vuex";
	import {formatDate} from '@/utils/util.js'
	import {edit} from '@/api/entrance.js'
	export default{
		name:"account",
		data(){
			return{
				username:"",
				action:"username"
			}
		},
		onLoad(option) {
			this.action = option.action || "username"
		},
		computed:{
			...mapState(["userInfo"]),
		},
		methods:{
			...mapActions(["setUserInfo_action"]),
			async updateUsername(){
				if(this.username === ""){
					this.$msg("请输入用户名")
					return;
				}
				if(this.username === this.userInfo.username){
					this.$msg("暂未更改")
					return;
				}
				let data = {action:this.action,username:this.username}
				let res = await edit(data);
				if(res.code === 1){
					this.setUserInfo_action();
					uni.navigateBack()
				}
			}
		}
	}
</script>

<style scoped lang="less" src="./account-edit.less">
</style>
